package com.fengke.cn.yejian11.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.fengke.cn.yejian11.R;
import com.fengke.cn.yejian11.base.BaseActivity;
import com.fengke.cn.yejian11.config.Config;
import com.fengke.cn.yejian11.impls.ActImpls;
import com.squareup.picasso.Downloader;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.fengke.cn.yejian11.R.id.user_change_name;

public class UserChangeActivity extends BaseActivity {

    private String user_id;
    private EditText user_name;
    private EditText user_password;
    private Button user_change1;
    private Button user_change2;
    String name;
    String password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_change);
        user_id=getUserId();
//        System.out.print(user_id);
//        user_name= (EditText) findViewById(user_change_name);
//        user_name.setText(user_id);
        user_change1= (Button) findViewById(R.id.user_change_button1);
        user_change2= (Button) findViewById(R.id.user_change_button2);
        user_name= (EditText) findViewById(R.id.user_change_name);
        user_password= (EditText) findViewById(R.id.user_change_password);
        user_change1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name=user_name.getText().toString();
                if (name.equals("")){
                    Toast.makeText(UserChangeActivity.this,"你的用户名不能为空",Toast.LENGTH_LONG).show();
                }else {
                    Retrofit retrofit=new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(Config.URL).build();
                    ActImpls services=retrofit.create(ActImpls.class);
                    Call<Integer> call=services.getUserChangeName(user_id,name);
                    call.enqueue(new Callback<Integer>() {
                        @Override
                        public void onResponse(Call<Integer> call, Response<Integer> response) {

//                            System.out.println("111111111111111111111111111111111111111111111111"+response.body());

                            if (response.body()==1){
                                Toast.makeText(UserChangeActivity.this,"用户名修改成功",Toast.LENGTH_LONG).show();
                            }else {
                                Toast.makeText(UserChangeActivity.this,"用户名修改shibai",Toast.LENGTH_LONG).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<Integer> call, Throwable t) {

                        }
                    });
                }
            }
        });

        user_change2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                password=user_password.getText().toString();
                if(password.equals("")){
                    Toast.makeText(UserChangeActivity.this,"您输入的密码为空",Toast.LENGTH_LONG).show();
                }else {
                    Retrofit retrofit=new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(Config.URL).build();
                    ActImpls services=retrofit.create(ActImpls.class);
                    Call<Integer> call=services.getUserChangePassword(user_id,password);
                    call.enqueue(new Callback<Integer>() {
                        @Override
                        public void onResponse(Call<Integer> call, Response<Integer> response) {
                            if (response.body()==1){
                                Toast.makeText(UserChangeActivity.this,"密码修改成功",Toast.LENGTH_LONG).show();
                            }else {
                                Toast.makeText(UserChangeActivity.this,"密码修改shibai",Toast.LENGTH_LONG).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<Integer> call, Throwable t) {

                        }
                    });
                }
            }
        });
    }
}
